package com.spring.crm.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.crm.conf.CommonResult;
import com.spring.crm.conf.ResultCode;
import com.spring.crm.entity.Contract;
import com.spring.crm.mapper.ContractMapper;
import javafx.application.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何卓
 * @since 2022-01-20
 */
@RestController
@RequestMapping("/api/crm/contract")
public class ContractController {

    private final static Logger LOGGER = LoggerFactory.getLogger(Application.class);

    @Resource
    private ContractMapper contractMapper;

    @CrossOrigin(origins = "*")
    @PostMapping("insert")
    public CommonResult insertOne(@RequestBody Contract record) {
        CommonResult rs;
        try {
//            if(record.getStartTime()==null){
//                return CommonResult.failed("名称不能为空");
//            }
            LocalDateTime dd = LocalDateTime.now(); // 注意是util包下的Date
            record.setCreateTime(dd.toString());
            int ret = contractMapper.insert(record);
            if (ret != 1){
                rs = CommonResult.failed();
            }else {
                JSONObject data = new JSONObject();
                data.put("contractId",record.getContractId());
                rs = CommonResult.success(data);
            }
        }catch(Exception ex){
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @GetMapping("condition")
    public CommonResult selectByCondition(@CookieValue(value = "auth",defaultValue = "") String auth,@RequestParam Map<String,String> map) {
        CommonResult rs;
        LOGGER.info("auth----------------------------");
        LOGGER.info(auth);
        try {
            QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
            if(map.containsKey("contractId")){
                queryWrapper.eq("contract_id",map.get("contractId"));
            }
            if(map.containsKey("customId")){
                queryWrapper.eq("custom_id",map.get("customId"));
            }
            if(map.containsKey("contractName")){
                queryWrapper.like("contract_name",map.get("contractName"));
            }
            if(map.containsKey("contractStatus")){
                queryWrapper.eq("contract_status",map.get("contractStatus"));
            }
            if(map.containsKey("salesId")){
                queryWrapper.eq("sales_id",map.get("salesId"));
            }
            //queryWrapper.orderByAsc("num");
            Page<Contract> page = new Page<>(Long.valueOf(map.get("current")),Long.valueOf(map.get("size")));
            IPage<Contract> SalesIPage = contractMapper.selectPage(page,queryWrapper);
            Integer selectCount = Math.toIntExact(contractMapper.selectCount(queryWrapper));
            List<Contract> list = SalesIPage.getRecords();
            rs = CommonResult.success(list,selectCount);
        }catch(Exception ex){
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @DeleteMapping ("delete")
    public CommonResult deleteOne(@RequestBody Contract record) {
        CommonResult rs;
        try {
            Contract contract = contractMapper.selectById(record.getContractId());
            if(contract==null){
                return CommonResult.failed(ResultCode.DATA_Not_Exist_ERROR);
            }
            int ret = contractMapper.deleteById(record.getContractId());
            if (ret != 1) {
                rs = CommonResult.failed();
            } else {
                rs = CommonResult.success("");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }

    @CrossOrigin(origins = "*")
    @PutMapping("update")
    public CommonResult updateOne(@RequestBody Contract record) {
        CommonResult rs;
        try {
            Contract contract = contractMapper.selectById(record.getContractId());
            if(contract==null){
                return CommonResult.failed(ResultCode.DATA_Not_Exist_ERROR);
            }
            int ret = contractMapper.updateById(record);
            if (ret != 1) {
                rs = CommonResult.failed();
            } else {
                JSONObject data = new JSONObject();
                data.put("contractId", record.getContractId());
                rs = CommonResult.success(data);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            rs = CommonResult.failed(ex.toString());
        }
        return rs;
    }
}
